package cn.zyx.org.controller;

import cn.zyx.basic.util.AjaxResult;
import cn.zyx.basic.util.PageList;
import cn.zyx.org.domain.SystemDictionaryDetail;
import cn.zyx.org.query.SystemDictionaryDetailQuery;
import cn.zyx.org.service.ISystemDictionaryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/systemDictionaryDetail")
public class SystemDictionaryDetailController {

    @Autowired
    private ISystemDictionaryDetailService systemDictionaryDetailService;

    /**
     * 添加和修改
     * @param systemDictionaryDetail
     */
    @PutMapping
    public AjaxResult save(@RequestBody SystemDictionaryDetail systemDictionaryDetail){
        try {
            if (systemDictionaryDetail.getId() == null){ // 添加
                systemDictionaryDetailService.add(systemDictionaryDetail);
            }else { // 修改
                systemDictionaryDetailService.update(systemDictionaryDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统异常!" + e.getMessage());
        }
    }

    /**
     * 删除
     * @param id
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            systemDictionaryDetailService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统异常!" + e.getMessage());
        }
    }

    /**
     * 分页查询数据
     * @param systemDictionaryDetailQuery
     * @return
     */
    @PostMapping
    public PageList<SystemDictionaryDetail> pageData(@RequestBody SystemDictionaryDetailQuery systemDictionaryDetailQuery){
        System.out.println(systemDictionaryDetailQuery);

        return systemDictionaryDetailService.getDetailById(systemDictionaryDetailQuery);
    }

    /**
     * 查询一条数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public SystemDictionaryDetail getById(@PathVariable("id") Long id){
        return systemDictionaryDetailService.getById(id);
    }


}
